<template>
  <div>
    <div class="logo">
      <img src="../assets/logo.png" alt="logo"  />
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link class="toreg" :to="{ name: 'Reg' }"
      >没有账户，去注册~</router-link
    >
  </div>
</template>

<script>
import { Toast } from "vant";
import { setToken } from "../utils/tools";
import { login } from "../services/auth";

export default {
  name: "Login",
  data() {
    return {
      userName: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(values) {
      const res = await login(values);
  // 手机号码
      const r = JSON.stringify(values.userName)
      
      if (res.code == "1") {
        setToken(res.data);
        localStorage.setItem('loginPhone',r)
        this.$router.push({
          name: "Home",
        });
      } else {
        Toast.fail(res.data);
       
      }
    },
  },
};
</script>

<style>
.logo {
  width: 100px;
  margin: 50px auto;
}
.toreg {
  display: block;
  width: 400px;
  color: red;
  margin: 10px auto;
  text-align: center;
}
</style>